<template>
  <div class="app-container">
    <el-page-header @back="goBack" v-if="!flag">
    </el-page-header>
    <el-form v-if="flag"
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="入库时间" style="width: 308px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="入库单编码" prop="warehousingCode">
          <el-input
            v-model.trim="queryParams.warehousingCode"
            style="width: 180px"
            placeholder="请输入入库单编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户名称" prop="customName">
          <el-input
            v-model.trim="queryParams.customName"
            style="width: 180px"
            placeholder="请输入客户名称"
            @keyup.enter="handleQuery"
          />
          <!-- <el-select v-model="queryParams.supplierName" filterable class="m-2" placeholder="请选择供应商"
                        style="width: 180px;">
                        <el-option v-for="item in supplierList" :key="item.pid" :label="item.name" :value="item.name" />
                    </el-select> -->
        </el-form-item>

        <div class="operation">
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button @click="advanced = !advanced">
            <a style="color: #448ef7">
              <el-icon v-if="advanced">
                <ArrowUpBold />
              </el-icon>
              <el-icon v-else>
                <ArrowDownBold />
              </el-icon>
            </a>
          </el-button>
          <right-toolbar
            style="margin-left: 10px"
            :search="false"
            v-model:showSearch="showSearch"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </div>
      </el-row>
      <el-row v-if="advanced">
        <el-form-item label="送货人" prop="deliveryMan">
          <el-input
            v-model.trim="queryParams.deliveryMan"
            style="width: 180px"
            placeholder="请输入送货人"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="纸张名称" prop="name">
          <el-input
            v-model.trim="queryParams.name"
            style="width: 180px"
            placeholder="请输入纸张名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="纸张编号" prop="code">
          <el-input
            v-model.trim="queryParams.code"
            style="width: 180px"
            placeholder="请输入纸张编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="定量" prop="weight">
          <el-input
            type="number"
            v-model.trim="queryParams.weight"
            style="width: 180px"
            placeholder="请输入定量"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-row>
          <el-form-item label="幅宽" prop="longWide">
            <el-input
              v-model.trim="queryParams.longWide"
              clearable
              style="width: 180px"
              placeholder="请输入幅宽"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="纸张品牌" prop="brand">
            <el-select
              v-model="queryParams.brand"
              clearable
              filterable
              placeholder="请选择纸张品牌"
              style="width: 180px"
            >
              <el-option
                v-for="item in mes_paper_brand"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="规格型号" prop="specs">
            <el-select
              v-model="queryParams.specs"
              clearable
              placeholder="选择规格型号"
              style="width: 180px"
            >
              <el-option
                v-for="item in mes_paper_specs"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="仓库" prop="name">
                    <el-input v-model.trim="queryParams.name" clearable style="width: 180px;" placeholder="请输入仓库"
                        @keyup.enter="handleQuery" />
                </el-form-item> -->
          <el-form-item label="厂/客料" prop="material">
            <el-select
              v-model="queryParams.material"
              clearable
              placeholder="选择厂/客料"
              style="width: 180px"
            >
              <el-option
                v-for="item in mes_paper_material"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-select
              v-model="queryParams.unit"
              clearable
              placeholder="选择单位"
              style="width: 180px"
            >
              <el-option
                v-for="item in mes_paper_unit"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-row>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="flag">
      <el-col :span="1.5">
                <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col>
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        :data="dataList"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          v-if="columns[0].visible"
          label="入库单编码"
          min-width="200"
          align="center"
          prop="warehousingCode"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleViewCode(row)" text>
              {{ row.warehousingCode }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column
          v-if="columns[1].visible"
          label="入库时间"
          min-width="120"
          align="center"
          prop="warehousingDate"
        />
        <el-table-column
          v-if="columns[2].visible"
          label="客/厂料"
          width="70"
          align="center"
          prop="paperType"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag
              :options="mes_paper_material"
              :value="scope.row.paperType"
            />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[4].visible"
          label="客户名称"
          width="90"
          align="center"
          prop="customName"
        />
        <el-table-column
          v-if="columns[5].visible"
          label="纸张编码"
          min-width="190"
          align="center"
          prop="code"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleViewPaperCoding(row)" text>
              {{ row.code }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[7].visible"
          label="纸张名称"
          min-width="120"
          align="center"
          prop="name"
        />
        <el-table-column
          v-if="columns[8].visible"
          label="规格"
          min-width="110"
          align="center"
          prop="specs"
        >
          <template #default="scope">
            <span v-if="scope.row.longWide">
              {{ scope.row.longWide }}
            </span>
            <span v-if="scope.row.specs">
              <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />
            </span>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[9].visible"
          min-width="90"
          label="品牌"
          align="center"
          prop="brand"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_brand" :value="scope.row.brand" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[10].visible"
          width="70"
          label="定量"
          align="center"
          prop="weight"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          v-if="columns[11].visible"
          width="70"
          label="单位"
          align="center"
          prop="unit"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_unit" :value="scope.row.unit" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[12].visible"
          align="center"
          width="90"
          prop="lnumber"
          :show-overflow-tooltip="true"
        >
          <template #header>
            <div>
              令/件
              <br />
              kg/卷筒
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column
                    v-if="columns[12].visible"
                    width="80"
                    label="幅宽"
                    align="center"
                    prop="longWide"
                /> -->
        <el-table-column
          v-if="columns[13].visible"
          align="center"
          width="90"
          prop="jnumber"
          :show-overflow-tooltip="true"
        >
          <template #header>
            <div>
              件数
              <br />
              卷筒数
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[14].visible"
          align="center"
          width="90"
          prop="odd"
          :show-overflow-tooltip="true"
        >
          <template #header>
            <div>
              零头(令)
              <br />
              零头(kg)
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[15].visible"
          align="center"
          width="120"
          fixed="right"
          prop="total"
          :show-overflow-tooltip="true"
        >
          <template #header>
            <div>
              总数量(令)
              <br />
              总数量(kg)
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[16].visible"
          label="送货人"
          align="center"
          prop="deliveryMan"
        />
        <el-table-column
          v-if="columns[17].visible"
          label="车牌号"
          align="center"
          prop="carNumber"
        />
        <el-table-column
          v-if="columns[18].visible"
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="order">
import { getDetaillist,exportWarehousingDetail } from "@/api/storage/warehouseReceipt.js";
import { listSupplier } from "@/api/basicInfo/supplier.js";
import { downLoadBlob } from '@/utils/ruoyi'
import useTagsViewStore from "@/store/modules/tagsView";
const tagsViewStore = useTagsViewStore();

const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const route = useRoute();
const supplierList = ref([]);
const total = ref(0);
const advanced = ref(false);
const totalSum = ref(0);
const flag=ref(true);

// 列显隐信息
const columns = ref([
  { key: 0, label: `入库单编码`, visible: true },
  { key: 1, label: `入库时间`, visible: true },
  { key: 2, label: `纸张类型`, visible: true },
  { key: 3, label: `供应商编码`, visible: true },
  { key: 4, label: `供应商名称`, visible: true },
  { key: 5, label: `纸张编码`, visible: true },
  { key: 6, label: `自有编码`, visible: true },
  { key: 7, label: `纸张名称`, visible: true },
  { key: 8, label: `规格型号`, visible: true },
  { key: 9, label: `品牌`, visible: true },
  { key: 10, label: `定量`, visible: true },
  { key: 11, label: `单位`, visible: true },
  { key: 12, label: `令/件`, visible: true },
  { key: 13, label: `件数`, visible: true },
  { key: 14, label: `零头(令)`, visible: true },
  { key: 15, label: `总数量(令)`, visible: true },
  { key: 16, label: `送货人`, visible: true },
  { key: 17, label: `车牌号`, visible: true },
  { key: 18, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 30,
  },
});

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);
const { mes_paper_material, mes_paper_specs, mes_paper_brand, mes_paper_unit } =
  proxy.useDict(
    "mes_paper_unit",
    "mes_paper_material",
    "mes_paper_specs",
    "mes_paper_brand"
  );

const getList = () => {
  loading.value = true;
  if(route.query?.code){
    queryParams.value.code=route.query.code;
    queryParams.value.startDate=route.query.startDate;
    queryParams.value.endDate=route.query.endDate;
    flag.value=false;
  }else{
    queryParams.value.startDate= Array.isArray(dateRange.value) ? dateRange.value[0] : undefined;
    queryParams.value.endDate=Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  }
  getDetaillist(queryParams.value)
    .then((res) => {
      loading.value = false;
      if (res.rows.length > 0) {
        totalSum.value = res.rows[0].totalSum;
      }
      dataList.value = res.rows.map((item) => {
        return {
          ...item,
          ...item.mesPaper,
          deliveryMan: item.mesWarehousingInfo.deliveryMan,
          carNumber: item.mesWarehousingInfo.carNumber,
          warehousingCode: item.mesWarehousingInfo.warehousingCode,
          warehousingDate: item.mesWarehousingInfo.warehousingDate,
          paperType: item.mesWarehousingInfo.paperType,
          supplierId: item.mesWarehousingInfo.supplierId,
          customName: item.mesWarehousingInfo.customName,
          remark: item.mesWarehousingInfo.remark,
        };
      });
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "total":
        sums[index] = totalSum.value;
        break;
      default:
        break;
    }
  });
  return sums;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};
// 入库单
const handleViewCode = (row) => {
  router.push({
    path: "/storage/warehouseReceipt/warehouseReceiptDetails",
    query: {
      id: row.warehousingid,
      info: true,
    },
  });
};
// 供应商
const handleViewSupplierCode = (row) => {
  router.push({
    path: "/basicInfo/supplier/details",
    query: {
      id: row.supplierId,
      readonly: true,
    },
  });
};
// 纸张
const handleViewPaperCoding = (row) => {
  router.push({
    path: "/basicInfo/paperCoding/details",
    query: {
      code: row.code,
      id: row.mesPaper.id,
      readonly: true,
    },
  });
};
const getListSupplier = () => {
  listSupplier({ pageNum: 1, pageSize: 9999 }).then((res) => {
    supplierList.value = res.rows;
  });
};
getList();
getListSupplier();

//  返回
const goBack = () => {
  setTimeout(() => {
    router.go(-1);
    tagsViewStore.delView(route);
  }, 0);
};

// 导出
const handleExport = () => {
  exportWarehousingDetail({
      ...queryParams.value,
      startDate: Array.isArray(dateRange.value) ? dateRange.value[0] : undefined,
      endDate:
        Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
    }).then(res => {
        downLoadBlob(res, '纸张入库明细')
    })
}
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}

// :deep() {
//   display: flex;
//   flex-direction: column;
// }
.el-table :deep(.el-table__body-wrapper) {
  order: 1;
}
</style>
